
package fileby.gui.sidebar;

import fileby.core.ActionKey;
import fileby.core.IDisplayableFile;
import fileby.core.SourceType;
import fileby.ctrl.sidebar.ISideBarItemListenerProvider;
import fileby.ctrl.sidebar.SideBarItemListener;
import fileby.ctrl.actions.IActionProvider;
import fileby.util.properties.IPropertyBundleProvider;
import fileby.util.properties.PropertyBundle;
import java.io.File;
import javax.swing.Action;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileSystemView;

/**
 *
 * @author Oscar
 */
class SideBarPanelItem extends javax.swing.JPanel {

    private IPropertyBundleProvider propertyBundleProvider;
    private ISideBarItemListenerProvider listenerProvider;
    private IActionProvider actionProvider;
    private IDisplayableFile file;
    private SourceType sourceType;
    private JPopupMenu popup;

    public SideBarPanelItem(IDisplayableFile file, SourceType sourceType,
            IPropertyBundleProvider propertyBundleProvider,
            ISideBarItemListenerProvider listenerProvider,
            IActionProvider actionProvider) {

        this.propertyBundleProvider = propertyBundleProvider;
        this.listenerProvider = listenerProvider;
        this.actionProvider = actionProvider;
        this.sourceType = sourceType;
        this.file = file;  
        initComponents();
        initCtrl();
    }
    
    private void initCtrl() {
        if (sourceType.equals(SourceType.FAVORITE)) {
            initJPopupMenu();
            SideBarItemListener l = listenerProvider.create();
            addMouseListener(l);
            popup.addPopupMenuListener(l);
        } else if (sourceType.equals(SourceType.COMPUTER)) {
            SideBarItemListener l = listenerProvider.create();
            addMouseListener(l);
        }
    }

    private void initJPopupMenu() {
        popup = new JPopupMenu();
        Action removeFromFavorites = actionProvider.getControl(ActionKey.REMOVE_FROM_FAVORITES);
        removeFromFavorites.putValue(ActionKey.REMOVE_FROM_FAVORITES.toString(), file.getAbsolutePath());
        popup.add(new JMenuItem(removeFromFavorites));
        setComponentPopupMenu(popup);
    }
  

    // <editor-fold defaultstate="collapsed" desc="Previously Generated Code">
    private void initComponents() {

        icon = new javax.swing.JLabel();
        name = new javax.swing.JLabel();

        PropertyBundle bundle = propertyBundleProvider.create(this.getClass());
        setBackground(bundle.getColor("Form.background")); // NOI18N
        setMaximumSize(new java.awt.Dimension(32767, 25));
        setToolTipText(file.getAbsolutePath());
        setBorder(new EmptyBorder(2,2,2,2));
        setName(file.getAbsolutePath());

        icon.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        icon.setIcon(FileSystemView.getFileSystemView().
                getSystemIcon(new File(file.getAbsolutePath())));

        name.setText(file.getName());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(icon, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(icon, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
    }// </editor-fold>

    private javax.swing.JLabel icon;
    private javax.swing.JLabel name;
 
}
